package com.real.operationalcontrols.企业列表;

import android.graphics.Rect;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.ToastUtils;
import com.google.gson.Gson;
import com.jzxiang.pickerview.TimePickerDialog;
import com.jzxiang.pickerview.data.Type;
import com.jzxiang.pickerview.listener.OnDateSetListener;
import com.previewlibrary.GPreviewBuilder;
import com.real.operationalcontrols.R;
import com.real.operationalcontrols.rxhttp.MyDialogObserver;
import com.real.operationalcontrols.rxhttp.RxHttpManger;
import com.real.operationalcontrols.基类.BaseActivity;
import com.real.operationalcontrols.实体类.PhotoListBean;
import com.real.operationalcontrols.实体类.UserViewInfo;
import com.real.operationalcontrols.工具包.DeleteItemListener;
import com.real.operationalcontrols.适配器.ImagesListAdapter;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;
import me.jessyan.autosize.internal.CancelAdapt;

public class PhotoActivity extends BaseActivity implements OnDateSetListener,
        CancelAdapt {
    @BindView(R.id.bar_back_btn)
    ImageView barBackBtn;
    @BindView(R.id.bar_title)
    TextView barTitle;
    @BindView(R.id.bar_check_btn)
    TextView barCheckBtn;
    @BindView(R.id.rv)
    RecyclerView rv;
    @BindView(R.id.qian_btn)
    TextView qianBtn;
    @BindView(R.id.time_btn)
    TextView timeBtn;
    @BindView(R.id.hou_btn)
    TextView houBtn;
    String start_time;
    String finsh_time;
    long baseTime;
    String id = "";
    ImagesListAdapter adapter;
    TimePickerDialog timePickerDialog;
    private GridLayoutManager mGridLayoutManager;
    private ArrayList<UserViewInfo> mThumbViewInfoList = new ArrayList<>();
    SimpleDateFormat sdf_hour = new SimpleDateFormat("yyyy-MM-dd");
    SimpleDateFormat sdf_start = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
    SimpleDateFormat sdf_finish = new SimpleDateFormat("yyyy-MM-dd 23:00:00");

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.activity_photo);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_photo;
    }

    @Override
    protected void getData() {
        barTitle.setText("智能抓拍");
        id = getIntent().getStringExtra("id");
        timeBtn.setText(sdf_hour.format(new Date()));
        baseTime = System.currentTimeMillis();
        initAdapter();
        initData();
    }

    private void initAdapter() {
        mGridLayoutManager = new GridLayoutManager(this, 4);
        rv.setLayoutManager(mGridLayoutManager);
        rv.setHasFixedSize(true);
        adapter = new ImagesListAdapter(this);
        rv.setAdapter(adapter);

        adapter.setItemListener(new DeleteItemListener() {
            @Override
            public void setDeleteItemListener(int position) {
                computeBoundsBackward(mGridLayoutManager.findFirstVisibleItemPosition());
                GPreviewBuilder.from(PhotoActivity.this)
                        .setData(mThumbViewInfoList)
                        .setCurrentIndex(position)
                        .setSingleFling(true)
                        .setType(GPreviewBuilder.IndicatorType.Number)
                        .start();
            }
        });
    }

    /**
     * 查找信息
     * 从第一个完整可见item逆序遍历，如果初始位置为0，则不执行方法内循环
     */
    private void computeBoundsBackward(int firstCompletelyVisiblePos) {
        for (int i = firstCompletelyVisiblePos; i < mThumbViewInfoList.size(); i++) {
            View itemView = mGridLayoutManager.findViewByPosition(i);
            Rect bounds = new Rect();
            if (itemView != null) {
                ImageView thumbView = (ImageView) itemView.findViewById(R.id.img_arlm);
                thumbView.getGlobalVisibleRect(bounds);
            }
            mThumbViewInfoList.get(i).setBounds(bounds);
        }
    }

    private void initData() {
        start_time = sdf_start.format(baseTime);
        finsh_time = sdf_finish.format(baseTime);
        Map<String, String> map = new HashMap<>();
        map.put("Start", start_time);
        map.put("End", finsh_time);
        map.put("MonitorID", "30");
        RxHttpManger.getInstance().get(this, "http://27.128.180.188:11114/dtgk/photo.asp", map,
                new MyDialogObserver(this) {
                    @Override
                    public void onSuccess(String response) {
                        PhotoListBean bean = new Gson().fromJson(response, PhotoListBean.class);
                        if (bean.isSuccess()) {
                            mThumbViewInfoList.clear();
                            for (int i = 0; i < bean.getData().size(); i++) {
                                mThumbViewInfoList.add(new UserViewInfo(bean.getData().get(i)));
                            }
                            adapter.setData(mThumbViewInfoList);
                        }
                    }
                });
    }

    @OnClick({R.id.bar_back_btn, R.id.qian_btn, R.id.time_btn, R.id.hou_btn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.bar_back_btn:
                finish();
                break;
            case R.id.qian_btn:
                baseTime -= 60 * 60 * 1000 * 24;
                timeBtn.setText(sdf_hour.format(baseTime));
                if (baseTime > System.currentTimeMillis()) {
                    return;
                }
                initData();
                break;
            case R.id.time_btn:
                initTimePickView(Type.YEAR_MONTH_DAY);
                break;
            case R.id.hou_btn:
                baseTime += 60 * 60 * 1000 * 24;
                timeBtn.setText(sdf_hour.format(baseTime));
                if (baseTime > System.currentTimeMillis()) {
                    ToastUtils.showShort("超出当前记录时间");
                    return;
                }
                initData();
                break;
        }
    }

    private void initTimePickView(Type type) {
        long tenYears = 10L * 365 * 1000 * 60 * 60 * 24L;
        timePickerDialog = new TimePickerDialog.Builder()
                .setCallBack(this)
                .setCancelStringId("取消")
                .setSureStringId("确认")
                .setTitleStringId("选择时间")
                .setYearText("年")
                .setMonthText("月")
                .setDayText("日")
                .setHourText("时")
                .setMinuteText("分")
                .setCyclic(false)
                .setMinMillseconds(System.currentTimeMillis() - tenYears)
                .setMaxMillseconds(System.currentTimeMillis())
                .setCurrentMillseconds(baseTime)
                .setThemeColor(getResources().getColor(R.color.colorTitle))
                .setType(type)
                .setWheelItemTextNormalColor(getResources().getColor(R.color.timetimepicker_default_text_color))
                .setWheelItemTextSelectorColor(getResources().getColor(R.color.colorTitle))
                .setWheelItemTextSize(12)
                .build();
        timePickerDialog.show(getSupportFragmentManager(), type.toString());
    }

    @Override
    public void onDateSet(TimePickerDialog timePickerView, long millseconds) {
        baseTime = millseconds;
        timeBtn.setText(sdf_hour.format(millseconds));
        initData();
    }
}
